class Hotkey {
    constructor(player) {
        this.player = player
        this.rightTimer = null
        this.enabled = player.property.hotkey
        this.player.__event.on('destroy', () => {
            this.close()
        })
        this.player.__event.on('canplay', () => {
            if (!this.enabled) return
            this.open()
        })
        this.player.__event.on('error', () => {
            this.close()
        })
    }

    open() {
        this.close()
        Hotkey.prototype.__handleKeydown = this.__bindKeydown.bind(this)
        Hotkey.prototype.__handleKeyup = this.__bindKeyup.bind(this)
        document.addEventListener('keydown', this.__handleKeydown)
        document.addEventListener('keyup', this.__handleKeyup)
        this.enabled = true
    }

    close() {
        document.removeEventListener('keydown', this.__handleKeydown)
        document.removeEventListener('keyup', this.__handleKeyup)
        this.enabled = false
    }

    __bindKeydown(e) {
        const tag = document.activeElement.tagName.toUpperCase()
        const editable = document.activeElement.getAttribute('contenteditable')
        if (tag === 'INPUT' || tag === 'TEXTAREA' || editable === '' || editable === 'true') return
        let event = e || window.event
        switch (event.keyCode) {
            case 27:
                event.preventDefault()
                if (this.player.container.classList.contains(('z-player-fulled'))) {
                    this.player.controller.fullWebButton?.click()
                }
                if (document.fullscreenElement || document['mozFullScreenElement'] ||
                    document['webkitFullscreenElement'] || document['msFullscreenElement']) {
                    this.player.controller.fullButton?.click()
                }
                break
            case 32:
                event.preventDefault()
                this.player.video.paused ? this.player.play() : this.player.pause()
                break
            case 38:
                event.preventDefault()
                this.player.volume(this.player.video.volume + 0.1, true)
                break
            case 40:
                event.preventDefault()
                this.player.volume(this.player.video.volume - 0.1, true)
                break
            case 37:
                event.preventDefault()
                this.player.seek(this.player.video.currentTime - this.player.property.jumpTime)
                this.player.notice(this.player.__translate('后退') + ` <<< ${this.player.property.jumpTime}s`)
                break
            case 39:
                event.preventDefault()
                if (this.rightDown) {
                    if (!this.rightTimer) this.player.notice(this.player.__translate('倍速播放') + ' >>>')
                    return
                }
                this.rightDown = true
                this.rightTimer = setTimeout(() => {
                    this.rightTimer = null
                    this.player.video.playbackRate *= 1.5
                }, 500)
                break
            case 77:
                event.preventDefault()
                if (!this.player.setting.visible) this.player.setting.open()
                else this.player.setting.close()
                break
            default: break
        }
    }

    __bindKeyup(e) {
        const tag = document.activeElement.tagName.toUpperCase()
        const editable = document.activeElement.getAttribute('contenteditable')
        if (tag === 'INPUT' || tag === 'TEXTAREA' || editable === '' || editable === 'true') return
        let event = e || window.event
        switch (event.keyCode) {
            case 39:
                event.preventDefault()
                if (!this.rightDown) return
                this.rightDown = false
                if (this.rightTimer) {
                    clearTimeout(this.rightTimer)
                    this.player.seek(this.player.video.currentTime + this.player.property.jumpTime)
                    this.player.notice(this.player.__translate('快进') + ` >>> ${this.player.property.jumpTime}s`)
                } else {
                    this.player.video.playbackRate /= 1.5
                }
                break
            default: break
        }
    }
}

export default player => new Hotkey(player)
